package com.easyfun.healthmagicbox.comm.json;

import org.json.JSONException;
import org.json.JSONObject;


public abstract class AbstractJsonWrapper<T extends JsonWrapper, E> implements
		JsonWrapper {
	E mpp;
	
	public AbstractJsonWrapper(E pp)
	{
		mpp = pp;
	}
	
	public AbstractJsonWrapper()
	{
	
	}
	
	public E getDataPojo() {
		return mpp;
	}
	
	public void deserialize(String serializedObj) throws JSONException {
		
		JSONObject obj = new JSONObject(serializedObj);
		deserializeFromObj(obj);
	}

	public String serialize() throws JSONException {
		return serializeToObj().toString();
	}

	static public <T extends JsonWrapper> T getInstance(JSONObject obj,
			Class<T> classType) throws JSONException, InstantiationException,
			IllegalAccessException {
		T tmp = classType.newInstance();
		tmp.deserializeFromObj(obj);
		return tmp;
	}

	static public <T extends JsonWrapper,E> T getInstance(String serializedObj,
			Class<T> classType) throws JSONException, InstantiationException,
			IllegalAccessException {
		T tmp = classType.newInstance();

		tmp.deserialize(serializedObj);
		return tmp;
	}

}
